#!/usr/bin/env python

from google.appengine.ext import db
from constants import UserConstants

import webapp2

#DEBUG
from inituser import inituser
from google.appengine.api import users

class User(db.Model):
    maxBuildings = UserConstants.maxBuildings
    buildingList = db.ListProperty(db.Key, verbose_name="Buildings Owned")
    userObject = db.UserProperty(verbose_name="Google User Object", auto_current_user_add=True)
    country = db.StringProperty(verbose_name="User's Country", default="AQ", required=True)
    faction = db.StringProperty(verbose_name="User's Faction", default="Unknown", required=True)
    salutation = db.StringProperty(verbose_name="Proper Title", default="Admiral", required=True)

#Allows placing of a new building
#Arguments:
#       user:   The user's id (datastore key)
#       type:   Type of building.  One of radar, silo, or airbase.
#       lat:    Latitude in DD.
#       lon:    Longitude in DD.
class NewBuilding(webapp2.RequestHandler):
    def get(self):
        try:
            lat = float(self.request.get("lat"));
            lon = float(self.request.get("lon"));
            alt = float(self.request.get("alt"));
        except:
            self.error(400)
            self.response.out.write("Error in parsing coordinates (error 0102)")
            return
        self.response.out.write()

    #Adds a building of the specified type
    def addBuilding(self, uid, type, lat, lon):
        #Get the appropriate user
        user = User.get(uid)
        if not user:
            return "User not found. (errno 0101)"
        user.foo()
        return

#Test function to create a new user
class NewUser(webapp2.RequestHandler):
    def post(self):
        uobj = inituser(users.get_current_user())
        self.response.out.write(str(uobj.key()))
        return



app = webapp2.WSGIApplication([
                               ('/newbuilding', NewBuilding),
                               ('/newuser', NewUser),
                               ], debug=True)